package com.cia.blockchain.service.impl;

import com.cia.blockchain.KeyValueStorage;
import com.cia.blockchain.model.Chaincode;
import com.cia.blockchain.service.Blockchainservice;
import org.fisco.bcos.sdk.BcosSDK;
import org.fisco.bcos.sdk.client.Client;
import org.springframework.context.ApplicationContext;

import org.fisco.bcos.sdk.client.protocol.response.BlockNumber;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.security.Key;

@Service
public class BlockchainserviceImpl implements Blockchainservice {
    private BcosSDK bcosSDK;
    private Client client;
    private KeyValueStorage keyValueStorage;

    private void initSDK() throws Exception{

        System.out.println("-----init BcosSDK:------");
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:fisco-config.xml");
        System.out.println("-----init config ok------");
        bcosSDK = context.getBean(BcosSDK.class);
        System.out.println("-----init BcosSDK ok------");
        // 为群组1初始化client
        client = bcosSDK.getClient(Integer.valueOf(1));
        System.out.println("-----init client ok------");
        // 向群组1部署KeyValueStorage合约
        CryptoKeyPair cryptoKeyPair = client.getCryptoSuite().getCryptoKeyPair();
        keyValueStorage = KeyValueStorage.deploy(client, cryptoKeyPair);

        System.out.println("-----deploy KeyValueStorage ok------:");
    }

    
    @Override
    public Chaincode read(String key){
        System.out.println("-----readchaincode------");
        Chaincode chaincode = new Chaincode();
        if(bcosSDK == null){
            try{
                initSDK();
            }catch (Exception e){
                System.out.println("error-initSDK:"+e.getMessage());
                return chaincode;
            }

        }
        if(bcosSDK == null || keyValueStorage == null){
            System.out.println("-----init BcosSDK failed------");
            return chaincode;
        }
        // 调用KeyValueStorage合约的get接口
        try{
            String getValue = keyValueStorage.get(key);
            chaincode.setValue(getValue);
            System.out.println("-----keyValueStorage value------:" + getValue);
        }catch (Exception e){
            System.out.println("error-getValue:"+e.getMessage());
            return chaincode;
        }
        chaincode.setKey(key);

        
        return chaincode;
    }
    

    @Override
    public boolean write(Chaincode chaincode){
        System.out.println("-----writechaicode------");
        if(bcosSDK == null){
            try{
                initSDK();
            }catch (Exception e){
                System.out.println("error-initSDK:"+e.getMessage());
                return false;
            }

        }
        if(bcosSDK == null || keyValueStorage == null){
            System.out.println("-----init BcosSDK failed------");
            return false;
        }
        // 调用KeyValueStorage合约的set接口
        String key = chaincode.getKey();
        String value = chaincode.getValue();
        TransactionReceipt receipt = keyValueStorage.write(key, value);
        if(receipt == null || receipt.equals("")){
            return false;
        }
        return true;
    }
    




}
